package com.arch.flink.biz.wc;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author pizhihui
 * @date 2020-07-30
 */
public class WordCountBeanDemo {

    private static FlatMapFunction<String, WordAndOne> STRING_SPLIT_ONE = (s, collector) -> {
        String[] s1 = s.split(" ");
        for (String s2 : s1) {
            collector.collect(new WordAndOne(s2, 1));

        }
    };

    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        DataStreamSource<String> ds = env.socketTextStream("localhost", 7777);

        SingleOutputStreamOperator<WordAndOne> so = ds
                .flatMap(STRING_SPLIT_ONE)
                .keyBy("word")
                .sum("word");

        so.print();

        env.execute("wordcount demo");

    }

}
